﻿using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.OAuth.Services.Common
{
    [EventSource(Name = "Sustainalytics-ClientPlatform-STS")]
    public sealed class STSEventSource : EventSource
    {
        public static Lazy<STSEventSource> _lazyLog = new Lazy<STSEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        public static STSEventSource Log => _lazyLog.Value;

        [NonEvent]
        public static STSEventSource InitLog()
        {
            var log = new STSEventSource();
            log.Ping();

            return log;
        }

        [Event(1, Level = EventLevel.LogAlways)]
        public void Ping()
        {
            if (IsEnabled())
            {
                WriteEvent(1);
            }
        }

        [Event(950, Level = EventLevel.Informational)]
        public void WriteSuceesfulIpLogin(string ip)
        {
            if (IsEnabled())
            {
                WriteEvent(950, ip);
            }
        }

        [Event(951, Level = EventLevel.Critical)]
        public void WriteFailureIpLogin(string msg)
        {
            if (IsEnabled())
            {
                WriteEvent(950, msg);
            }
        }

        [Event(952, Level = EventLevel.Critical)]
        public void WriteExceptionIpLogin(string msg)
        {
            if (IsEnabled())
            {
                WriteEvent(950, msg);
            }
        }

        #region SignalRHub

        [Event(1400, Level = EventLevel.Verbose, Message = "STS hub notifies {0} clients")]
        public void SignalRHubNotifiedClients(int count)
        {
            if (IsEnabled())
            {
                WriteEvent(1400, count);
            }
        }

        [Event(1401, Level = EventLevel.Verbose, Message = "STS hub starts notifications to browser clients.")]
        public void SignalRHubNotifyClients()
        {
            if (IsEnabled())
            {
                WriteEvent(1401);
            }
        }

        [Event(1402, Level = EventLevel.Verbose, Message = "STS hub remove for hub client {0} connection id: {1}, final connection count {2}")]
        public void SignalRHubRemoveClients(string username, string connectionid, int count)
        {
            if (IsEnabled())
            {
                WriteEvent(1402, username, connectionid, count);
            }
        }

        [Event(1403, Level = EventLevel.Verbose, Message = "STS hub add or update for hub client {0} connection id: {1},  final connection count {2}")]
        public void SignalRHubAddOrUpdateClients(string username, string connectionid, int count)
        {
            if (IsEnabled())
            {
                WriteEvent(1403, username, connectionid, count);
            }
        }

        [Event(1404, Level = EventLevel.Verbose, Message = "STS hub  reconnect for user {0} connection id {1}. ")]
        public void SignalRHubOnReconnected(string username, string connectionid)
        {
            if (IsEnabled())
            {
                WriteEvent(1404, username, connectionid);
            }
        }

        [Event(1405, Level = EventLevel.Verbose, Message = "STS hub  disconnect  for user {0} connection id {1}. ")]
        public void SignalRHubOnDisconnected(string username, string connectionid)
        {
            if (IsEnabled())
            {
                WriteEvent(1405, username, connectionid);
            }
        }

        [Event(1406, Level = EventLevel.Verbose, Message = "STS hub  disconnect  for user {0} connection id {1}. ")]
        public void SignalRHubOnConnected(string username, string connectionid)
        {
            if (IsEnabled())
            {
                WriteEvent(1406, username, connectionid);
            }
        }

        #endregion
    }
}
